<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-row :gutter="1">
      <el-form>
        <el-col :span="10"
          ><div>
            <el-form-item label="课程类别">
              <el-select
                v-model="searchObj.subjectParentId"
                placeholder="请选择"
                @change="subjectTwoListChanged"
                clearable="true"
              >
                <el-option
                  v-for="item in subjectList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="searchObj.subjectId"
                placeholder="请选择"
                clearable="true"
              >
                <el-option
                  v-for="item in subjectTwoList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item></div
        ></el-col>
        <el-col :span="5">
          <el-form-item label="标题">
            <el-input
              v-model="searchObj.title"
              placeholder="请输入标题"
              style="width: 200px;"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="讲师">
            <el-select
              v-model="searchObj.teacherId"
              placeholder="请选择"
              clearable="true"
            >
              <el-option
                v-for="item in teacherList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="fetchData()"
              size="medium"
              >搜索</el-button
            >
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <!-- 表格 -->

    <el-table :data="courseList" stripe style="width: 100%" border>
      <el-table-column
        header-align="center"
        align="center"
        label="序号"
        width="100"
      >
        <template slot-scope="scope">
          {{ (currentPage - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="封面" width="220">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="scope.row.title" width="100%" />
        </template>
      </el-table-column>
      <el-table-column label="课程信息" width="200">
        <template slot-scope="scope">
          <a href="">{{ scope.row.title }}</a>
          <p>
            分类：{{ scope.row.subjectParentTitle }}>{{
              scope.row.subjectTitle
            }}
          </p>
          <p>
            课时：{{ scope.row.lessonNum }}/ 浏览：{{ scope.row.viewCount }}/
            付费学员：{{ scope.row.buyCount }}
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="teacherName" label="讲师" width="100">
      </el-table-column>
      <el-table-column label="价格" width="100%" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="Number(scope.row.price === 0)"
            >免费</el-tag
          >
          <el-tag v-else>{{ scope.row.price }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="课程状态"
        prop="status"
        align="center"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === 'Draft' ? 'warning' : 'success'"
            >{{ scope.row.status }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.gmtCreate.substr(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <router-link :to="'/course/info/' + scope.row.id">
            <el-button type="primary" size="mini">修改</el-button>
          </router-link>

          <el-button type="danger" size="mini" @click="removeById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="sizes,prev, pager, next, jumper, ->, total"
      :total="total"
      :page-sizes="[5, 10, 20]"
      :page-size="size"
      :current-page="currentPage"
      style="padding: 30px 0;"
    >
    </el-pagination>
  </div>
</template>

<script>
import teacher from "../../api/teacher";
import course from "../../api/course";
import subject from "../../api/subject";
export default {
  data() {
    return {
      courseList: [], //课程列表
      total: 0,
      size: 10,
      currentPage: 1,
      searchObj: {
        subjectId: "",
      }, //查询条件
      teacherList: [],
      subjectList: [], //一级分类列表
      subjectTwoList: [], //二级分类列表
    };
  },
  mounted() {
    this.fetchData();
    this.getTeacherList();
    this.getSubjectList();
  },
  methods: {
    handleSelectionChange(val) {
      this.courseList = val;
    },
    handleSizeChange(val) {
      this.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    /**
     * 课程列表
     */

    fetchData() {
      course
        .getCoursePage(this.currentPage, this.size, this.searchObj)
        .then((resp) => {
          if (resp) {
            console.log(resp.data);
            this.courseList = resp.data.item.data;
            this.total = resp.data.item.total;
          }
        });
    },
    /**
     * 讲师列表
     */
    getTeacherList() {
      teacher.list().then((resp) => {
        if (resp) {
          this.teacherList = resp.data.items;
        }
      });
    },
    /**
     * 课程分类列表
     */
    getSubjectList() {
      subject.getSubjectTreeList().then((resp) => {
        if (resp) {
          this.subjectList = resp.data.items;
        }
      });
    },
    /**
     * 切换二级类别
     */

    subjectTwoListChanged(val) {
      for (let i = 0; i < this.subjectList.length; i++) {
        if (this.subjectList[i].id == val) {
          this.subjectTwoList = this.subjectList[i].children;
          this.searchObj.subjectId = "";
        }
      }
    },
    /**
     * 根据id删除课程
     * @param {*} id
     */
    removeById(id) {
      this.$confirm(
        "此操作将永久删除该课程，该课程下的视频以及所有的相关信息，是否继续？",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          course.removeCourseById(id).then((resp) => {
            if (resp) {
              this.$message.success(resp.message);
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message.info("以取消删除！");
        });
    },
  },
};
</script>

<style></style>
